package com.arsn.myf.system.controller;

import com.arsn.myf.common.exception.CustomException;
import com.arsn.myf.common.util.result.Func;
import com.arsn.myf.common.util.result.R;
import com.arsn.myf.system.service.RegionService;
import com.arsn.myf.system.vo.DeptVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import liquibase.pro.packaged.Q;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wildfly.common.annotation.NotNull;
import java.util.Arrays;
import java.util.List;

import com.arsn.myf.system.vo.RegionVO;
import com.arsn.myf.system.entity.Region;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 行政区划 控制层
 * @author zhongrj
 * @date 2022-12-12 21:41:22
 */
@RestController
@RequestMapping("/system/region")
@AllArgsConstructor
@Api(value = "行政区划", tags = "行政区划")
public class RegionController{

    private final RegionService regionService;

    /**
     * 查询列表分页信息
     * @param page
     * @param region
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询列表分页信息")
    public R list(Page<Region> page,Region region) {
        return R.data(regionService.page(page, new QueryWrapper<>(region)));
    }

    /**
     * 自定义查询分页信息
     * @param page
     * @param region
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "自定义查询分页信息")
    public R page(Page<RegionVO> page, RegionVO region) {
        return R.data(regionService.selectPageRegionList(page, region));
    }

    /**
     * 详细信息
     * @param region
     * @return
     */
    @GetMapping(value = "/detail" )
    @ApiOperation(value = "详细信息" )
    public R getDetail(Region region) {
        return R.data(regionService.getOne(new QueryWrapper<>(region)));
    }


    /**
     * 自定义查询详细信息
     * @param region
     * @return
     */
    @GetMapping(value = "/getRegionDetail" )
    @ApiOperation(value = "自定义查询详细信息" )
    public R getRegionDetail(Region region) {
        return R.data(regionService.getRegionDetail(region));
    }

    /**
     * 新增
     * @param region
     * @return
     */
    @PostMapping("/save" )
    @ApiOperation(value = "新增" )
    public R save(@RequestBody Region region) {
        //判断编号是否重复
        QueryWrapper<Region> wrapper = new QueryWrapper<>();
        wrapper.eq("code", region.getCode());
        List<Region> list = regionService.list(wrapper);
        if (list.size()>0){
            throw new CustomException("区域编号已存在!");
        }
        return R.status(regionService.save(region));
    }

    /**
     * 修改
     * @param region
     * @return
     */
    @PostMapping("/update" )
    @ApiOperation(value = "修改" )
    public R update(@RequestBody Region region) {
        //判断编号是否重复
        QueryWrapper<Region> wrapper = new QueryWrapper<>();
        wrapper.eq("code", region.getCode());
        List<Region> list = regionService.list(wrapper);
        if (list.size()>0){
            throw new CustomException("区域编号已存在!");
        }
        return R.status(regionService.updateById(region));
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @PostMapping("/remove" )
    @ApiOperation(value = "删除" )
    public R remove(@NotNull String ids) {
        return R.status(regionService.removeByIds(Func.stringToLong(Arrays.asList(ids.split("," )))));
    }

    /**
     * 懒加载区域列表
     * @param region 区域参数实体
     * @return
     */
    @GetMapping("/getRegionLazyList")
    @ApiOperation(value = "懒加载区域列表")
    public R getRegionLazyList(RegionVO region){
        return R.data(regionService.getRegionLazyList(region));
    }
}
